package com.common.controller;

import com.common.enums.ApiResult;
import com.common.model.SystemUserModel;
import com.common.mysql.SearchPageResult;
import com.common.mysql.Where;
import com.common.sevice.SystemUserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/v1/system-user")
public class SystemUserController extends Controller<String, SystemUserService, SystemUserModel> {

    @Override
    public ApiResult searchPage(@RequestParam(defaultValue = "1", required = false) int page,
                                @RequestParam(defaultValue = "10", required = false) int limit,
                                @RequestParam Map<String, Object> params) {
        SearchPageResult<SystemUserModel> result = new SearchPageResult<>();
        result.setPage(page);
        result.setLimit(limit);
        Where where = Where.build().field("del_flag").equal(0);
        if(params.containsKey("name")){
            where.and().field("name").equal(params.get("name"));
        }
        if(params.containsKey("userName")){
            where.and().field("user_name").equal(params.get("userName"));
        }
        List<SystemUserModel> resultList = service.search(where.limit(page, limit));
        result.setList(resultList);
        int totalCount = service.count(Where.build().field("del_flag").equal(0));
        result.setTotalCount(totalCount);
        return ApiResult.success(result);
    }
}
